"use client";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { useState } from "react";
import { MdDeleteOutline } from "react-icons/md";
import { trpc } from "@/app/_trpc/client";
import { AreaSelect } from "@/database/schema";
import { SelectedArea } from "../types";
import Loading from "@/components/Loading";

const AlertDeleteArea = ({
  selectedArea,
}: {
  selectedArea: Partial<SelectedArea | null>;
}) => {
  const [open, setOpen] = useState(false);
  const utils = trpc.useUtils();
  const { mutate, isPending } = trpc.areas.deleteProjectArea.useMutation({
    onSuccess: () => {
      setOpen(false);
      utils.projects.getProjectById.invalidate({value:selectedArea?.projectId});
    },
  });

  return (
    <AlertDialog open={open} onOpenChange={setOpen}>
      <AlertDialogTrigger>
        <MdDeleteOutline className="text-2xl text-red-600" />
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>
            Seguro que desea eliminar este articulo?
          </AlertDialogTitle>
          <AlertDialogDescription>
            Esta accion no puede ser reversible, y será borrada permanentemente
            de este projecto
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel className="border-none">
            <Button variant="link">Cancelar</Button>
          </AlertDialogCancel>
          <Button
            className="bg-primary text-white"
            disabled={isPending}
            onClick={() => {
              mutate({
                id: selectedArea?.id!,
              });
            }}
          >
            <span className="mr-2"> {isPending ? "Eliminando" : "Eliminar"}</span>
            {isPending && <Loading size={15} />}
          </Button>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
};

export default AlertDeleteArea;
